package com.lyl.momanapp.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lyl.momanapp.R;
import com.lyl.momanapp.bean.ProductTemplates;
import com.lyl.momanapp.bean.VariableID;

import org.xutils.x;

import java.util.ArrayList;

/**
 * 名字 梁亚龙
 * 时间 2017/10/17.
 * QQ邮箱 503934353@qq.com
 */

public class Shop extends BaseAdapter {
    private int num = -1;
    private LayoutInflater mInflater;
    private Context context;
    private ArrayList<ProductTemplates.ResultBean.DataBean> list = new ArrayList<>();

    public Shop(Context context) {
        this.mInflater = LayoutInflater.from(context);

        this.context = context;
    }

    public void showData(ArrayList<ProductTemplates.ResultBean.DataBean> list) {
        this.list.addAll(list);
        this.notifyDataSetChanged();

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_shop, null);
            viewHolder.imageView = convertView.findViewById(R.id.shop_item_iv);
            viewHolder.name = convertView.findViewById(R.id.shop_item_name);
            viewHolder.price = convertView.findViewById(R.id.shop_item_price);
            viewHolder.background = convertView.findViewById(R.id.shop_item_background);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final ProductTemplates.ResultBean.DataBean productTemplates = list.get(position);
        viewHolder.name.setText(productTemplates.getProduct_temp_name());
        viewHolder.price.setText(productTemplates.getPreferential_price());
        x.image().bind(viewHolder.imageView, productTemplates.getOriginal_image().get(0));
        if (num == position) {
            viewHolder.background.setBackgroundColor(Color.parseColor("#F8DB42"));
            VariableID.product_temp_id = productTemplates.getProduct_temp_id();
        } else {
            viewHolder.background.setBackgroundColor(Color.parseColor("#ffffff"));
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                num = position;
                Shop.this.notifyDataSetChanged();
                mOnItemClickLitener.onItemClick(productTemplates);
            }
        });

        return convertView;
    }

    class ViewHolder {
        private LinearLayout background;
        private TextView name, price;
        private ImageView imageView;
    }

    public interface OnItemClickLitener {
        void onItemClick(ProductTemplates.ResultBean.DataBean productTemplates);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}
